<?php

/**
 * @var $submit Zend_Form_Element_Submit
 */
abstract class Contest_Form_Abstract extends YouNet_Form
{
	const URL_PATTERN				= '/(news|telnet|nttp|file|http|ftp|https):\/\/([\w\d\-]+\.){1,}[A-Z a-z]{2,}/';//'/^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&amp;?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/';
	const SUBFORM_ACTION_BUTTON		= 'actionButton';
	const GROUP_NAME_ACTION_BUTTON  = 'submitButtons';
	const BACK_BUTTON_NAME			= 'back';
	const BACK_BUTTON_CANCEL		= 'cancel';
	const BACK_BUTTON_SAVE			= 'submit';
	const FANPAGE_NAME				= 'fanpages';

	protected $_element_decorators = array(
        'ViewHelper',
		'Description',
		'Errors',
		array('HtmlTag', array('tag' => 'dd')),
		array('Label', array('tag' => 'dt')),
    );

    protected $_file_decorators = array(
    	'File',
		'Description',
		'Errors',
		array('HtmlTag', array('tag' => 'dd')),
		array('Label', array('tag' => 'dt')),
    );

	protected abstract function _initBegin();
	protected abstract function _initFields();
	protected abstract function _hasBackButton();
	public abstract function disableFields();

	public abstract function fill(Core_Model_Contest $contestRow);
	
	/**
	 *
	 * @var Core_Model_Contest
	 */
	protected $_contestRow		= null;
	
	final public function init()
	{
		parent::init();

		$this->setAttrib('class', 'sm_form standard');
		$this->setName('contest-form');
		
		$this->_initBegin();
		$this->_initFields();
		$this->_initEnd();
    }
    
	/*
     * remove button cancel and button
     */
    protected function removeButton()
    {
        $this->_getActionButtonsGroup()->removeElement(self::BACK_BUTTON_CANCEL);
        $this->_getActionButtonsGroup()->removeElement(self::BACK_BUTTON_NAME);
    }

    /*
     * rename button 
     */
    protected function renameButton($id, $name)
    {
        $this->getSubForm(self::SUBFORM_ACTION_BUTTON)->getElement($id)->setLabel($name);
    }

    protected function _initEnd()
	{
		$subForm = new YouNet_Form_SubForm();
		$subForm->addElement('submit', self::BACK_BUTTON_SAVE, array(
            'label'			=> $this->getTranslator()->_('BUTTON_ADD'),
            'onclick'		=> 'window.isSubmitting = true;',
            'decorators'	=> array(
                'ViewHelper',
            ),
            'class'			=> 'greenbutton'
        ));
        $subForm->addElement('button', self::BACK_BUTTON_CANCEL, array(
            'label'			=> $this->getTranslator()->_('BUTTON_CANCEL'),
            'class'			=> 'graybutton',
            'onclick'		=> 'window.isSubmitting = true;top.location.href= "'.$this->getCancelUrl().'";',
            'decorators' => array(
                'ViewHelper',
            ),
        ));
        $subForm->addDisplayGroup(array(self::BACK_BUTTON_SAVE, self::BACK_BUTTON_CANCEL), self::GROUP_NAME_ACTION_BUTTON, array(
            'decorators' => array(
                'FormElements',
				'DtDdWrapper'
            ),
        ));
		$this->addSubForm($subForm, self::SUBFORM_ACTION_BUTTON);

		if( $this->_hasBackButton() )
		{
			$this->_addBackButton();
		}
	}

	protected function getCancelUrl()
	{
		return $this->_social->url('/contest/manage/list');
	}

	private function _addBackButton()
	{
		$backButton = new Zend_Form_Element_Button(self::BACK_BUTTON_NAME, array(
			'label'			=> $this->getTranslator()->_('BUTTON_BACK'),
			'class'			=> 'graybutton',
			'decorators' => array(
				'ViewHelper',
			),
		));
		$backButton->setOrder(1);
		$this->_getActionButtonsGroup()->addElement($backButton);
	}
	/**
	 *
	 * @return Zend_Form_DisplayGroup
	 */
	protected function _getActionButtonsGroup()
	{
		$subForm = $this->getSubForm(self::SUBFORM_ACTION_BUTTON);
		return $subForm->getDisplayGroup(self::GROUP_NAME_ACTION_BUTTON);
	}
	
	/**
	 *
	 * @param string $method_action
	 * @param int $id_contest
	 * @return Contest_Form_Abstract 
	 */
	public function setSubmitAction($prefix, $method_action, $id_contest = 0)
	{
		$action = $prefix.'/'.$method_action;
		if( !empty($id_contest) )
		{
			$action .= '/id/'.$id_contest;
		}
		parent::setAction($action);
		return $this;
	}
	
	public function setBackAction($prefix, $method_action, $id_contest = 0)
	{
		if( !$this->_hasBackButton() )
		{
			throw new Zend_Exception("The form has no Back button");
		}
		/* @var $backButton Zend_Form_Element_Button */
		$backButton = $this->_getActionButtonsGroup()->getElement(self::BACK_BUTTON_NAME);
		$action = $prefix.'/'.$method_action;
		if( !empty($id_contest) )
		{
			$action .= '/id/'.$id_contest;
		}
		$backButton->setAttrib('onclick', 'window.isSubmitting = true;top.location.href = "'.$action.'"');
	}
	
	public function setContest(Core_Model_Contest $contest)
	{
		$this->_contestRow = $contest;
	}
};